<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Validator;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Validator::extend('phone', function ($attribute, $value, $parameters) {
            //准备手机号码正则
            $reg = '/^1\d{10}$/';
            return preg_match($reg, $value);
        });
        Validator::extend('page_theme', function ($attribute, $value, $parameters) {
            $tmpPageThemes = glob(config('app.page_theme'));
            foreach ($tmpPageThemes as $theme) {
                $pageThemes[] = explode('.', basename($theme))[0];
            }

            return array_search($value, $pageThemes) !== false;
        });
        Validator::extend('chunk_theme', function ($attribute, $value, $parameters) {
            $tmpChunkThemes = glob(config('app.chunk_theme'));
            foreach ($tmpChunkThemes as $theme) {
                $chunkThemes[] = explode('.', basename($theme))[0];
            }

            return array_search($value, $chunkThemes) !== false;
        });
        Validator::extend('sort_theme', function ($attribute, $value, $parameters) {
            $tmpSortThemes = glob(config('app.sort_theme'));
            foreach ($tmpSortThemes as $theme) {
                $sortThemes[] = explode('.', basename($theme))[0];
            }

            return array_search($value, $sortThemes) !== false;
        });
        Validator::extend('info_theme', function ($attribute, $value, $parameters) {
            $tmpInfoThemes = glob(config('app.info_theme'));
            foreach ($tmpInfoThemes as $theme) {
                $infoThemes[] = explode('.', basename($theme))[0];
            }

            return array_search($value, $infoThemes) !== false;
        });
        Validator::extend('repwd', function ($attribute, $value, $parameters) {
            return \Hash::check($value, \App\Set::where('name', 'repwd')->value('value'));
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
